package com.platform.qujia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.qujia.dao.GradeQrSettingMapper;
import com.platform.qujia.enums.GradeExpireMode;
import com.platform.qujia.pojo.AppUserInfo;
import com.platform.qujia.pojo.Enterprise;
import com.platform.qujia.pojo.GradeQrSetting;
import com.platform.qujia.pojo.UserVirtualGradeRel;
import com.platform.qujia.pojo.vo.GradeQrSettingVO;
import com.platform.qujia.service.AppUserInfoService;
import com.platform.qujia.service.EnterpriseService;
import com.platform.qujia.service.IGradeQrSettingService;
import com.platform.qujia.service.IUserVirtualGradeRelService;
import com.platform.qujia.utils.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @date 2024/1/16
 */
@Service
public class GradeQrSettingServiceImpl extends ServiceImpl<GradeQrSettingMapper, GradeQrSetting> implements IGradeQrSettingService {

    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private IUserVirtualGradeRelService userVirtualGradeRelService;
    @Autowired
    private AppUserInfoService appUserInfoService;

    @Override
    public GradeQrSetting querySetting(GradeQrSetting setting, LocalDateTime time) {
        QueryWrapper<GradeQrSetting> qw = Wrappers.query(setting);
        qw.last("limit 1");
        GradeQrSetting one = getOne(qw);
        if (one != null) {
            if (one.getExpireMode()==2) {
                LocalDate nowDate = time.toLocalDate();
//                LocalTime nowTime = time.toLocalTime();
                if(nowDate.isBefore(one.getEffectStartDate()) || nowDate.isAfter(one.getEffectEndDate())){
                    one = null;
                }
            }
        }
        return one;
    }

    @Override
    public GradeQrSettingVO querySettingVO(GradeQrSetting setting, LocalDateTime time) {
        GradeQrSetting qrSetting = querySetting(setting, time);
        if (qrSetting == null) {
            return null;
        }
        Enterprise enterprise = enterpriseService.selectEnterpriseInfo(Math.toIntExact(qrSetting.getEnterpriseId()));
        if (enterprise == null) {
            return null;
        }
        GradeQrSettingVO settingVO = new GradeQrSettingVO();
        settingVO.setSetting(qrSetting);
        settingVO.setEnterpriseName(enterprise.getEnterpriseName());
        return settingVO;
    }

    @Override
    public UserVirtualGradeRel joinGrade(Long userId, Long enterpriseId, String gradeCode, Long version) {
        AppUserInfo userInfo = appUserInfoService.getById(Math.toIntExact(userId));
        if (userInfo==null || userInfo.getStationId() != Math.toIntExact(enterpriseId)) {
            throw new CustomException("用户不存在");
        }
        GradeQrSetting form = new GradeQrSetting();
        form.setEnterpriseId(enterpriseId);
        form.setGradeCode(gradeCode);
        form.setVersion(version);
        LocalDateTime now = LocalDateTime.now();
        GradeQrSetting setting = querySetting(form, now);
        if (setting == null) {
            throw new CustomException("二维码已失效");
        }
        UserVirtualGradeRel f1 = new UserVirtualGradeRel();
        f1.setEnterpriseId(enterpriseId);
        f1.setUserId(userId);
        QueryWrapper<UserVirtualGradeRel> query = Wrappers.query(f1);
        query.last("limit 1");
        UserVirtualGradeRel userVirtualGradeRel = userVirtualGradeRelService.getOne(query);
        boolean u = true;
        if (userVirtualGradeRel == null) {
            userVirtualGradeRel = new UserVirtualGradeRel();
            userVirtualGradeRel.setEnterpriseId(enterpriseId);
            userVirtualGradeRel.setUserId(userId);
            u = false;
        }
        userVirtualGradeRel.setUpdateTime(now);
        userVirtualGradeRel.setGradeCode(gradeCode);
        if (setting.getExpireMode()==1) {
            userVirtualGradeRel.setExpireMode(GradeExpireMode.FIXED_DAY);
            userVirtualGradeRel.setStartTime(now);
            userVirtualGradeRel.setExpireTime(now.plusDays(setting.getFixedDay()));
            userVirtualGradeRel.setEffectStartDate(null);
            userVirtualGradeRel.setEffectEndDate(null);
            userVirtualGradeRel.setEffectStartTime(null);
            userVirtualGradeRel.setEffectEndTime(null);
        }else{
            userVirtualGradeRel.setExpireMode(GradeExpireMode.TIME_SLOT);
            userVirtualGradeRel.setStartTime(null);
            userVirtualGradeRel.setExpireTime(null);
            userVirtualGradeRel.setEffectStartDate(setting.getEffectStartDate());
            userVirtualGradeRel.setEffectEndDate(setting.getEffectEndDate());
            userVirtualGradeRel.setEffectStartTime(setting.getEffectStartTime());
            userVirtualGradeRel.setEffectEndTime(setting.getEffectEndTime());
        }
        u = u ? userVirtualGradeRelService.updateById(userVirtualGradeRel) : userVirtualGradeRelService.save(userVirtualGradeRel);
        return userVirtualGradeRel;
    }
}
